package com.thinkwide.data.core;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class MFFile {
    private boolean readXML(String xml) throws DocumentException {
        Document doc = DocumentHelper.parseText(xml);

        Element root = doc.getRootElement();
        Element foo;

        for (Iterator i = root.elementIterator("VALUE"); i.hasNext(); ) {
            foo = (Element) i.next();
            System.out.print(foo.elementText("NO"));
            System.out.println(foo.elementText("ADDR"));
        }
        return true;

    }

    public static Object readJsonToObjectExt(String json) {
        return json;

    }

    public static String writeObjectToJsonExt(Object obj) {
        return "";
    }

    public static String writeObjectToXMLExt(Object obj) {
        return "";
    }

    public static Object readXMLToObject(String xml) throws DocumentException {
        if (MFString.isNullOrEmpty(xml)) return null;

        ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes());
        XMLDecoder decoder = new XMLDecoder(stream);
        Object obj = null;
        obj = decoder.readObject();
        decoder.close();
        return obj;
    }


    public static String writeObjectToXML(Object obj) throws DocumentException {
        if (obj == null) return null;

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(stream);
        encoder.writeObject(obj);
        encoder.flush();
        encoder.close();
        return stream.toString();
    }

    public static String readTextFile(String filePath) throws IOException {
        return readTextFile(filePath, Charset.defaultCharset().name());
    }


    public static String readTextFileCode(String filePath) throws IOException {
        BufferedInputStream bin = new BufferedInputStream(
                new FileInputStream(filePath));
        int p = (bin.read() << 8) + bin.read();
        String code = null;
        switch (p) {
            case 0xefbb:
                code = "UTF-8";
                break;
            case 0xfffe:
                code = "Unicode";
                break;
            case 0xfeff:
                code = "UTF-16BE";
                break;
            default:
                code = "GBK";
        }

        return code;
    }

    public static String readTextFile(String filePath, String charsetName) throws IOException {

        StringBuilder sb = new StringBuilder();
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), charsetName);
            BufferedReader bufferedReader = new BufferedReader(read);
            String strLine = null;
            //bufferedReader.read();
            while ((strLine = bufferedReader.readLine()) != null) {
                sb.append(strLine).append("\r\n");
            }

            read.close();
        }
        return sb.toString();

    }

    public static boolean saveTextFile(String filePath, String str) throws IOException {
        return saveTextFile(filePath, str, Charset.defaultCharset().name());
    }

    public static boolean saveTextFile(String filePath, String str, String charsetName) throws IOException {
        File file = new File(filePath);
        OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file), charsetName);
        //FileWriter write=new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(write);
        bufferedWriter.write(str);
        bufferedWriter.flush();
        write.close();
        return true;
    }


    public static Date getFileModifyDate(String filePath) {
        File file = new File(filePath);
        long modifiedTime = file.lastModified();
        Date date = new Date(modifiedTime);
        return date;
    }

    public static String getFileExt(String filePath) {
        String ext = "";
        int i = filePath.lastIndexOf(".");
        if (i > 0) ext = filePath.substring(i);
        return ext;
    }

    public static String getFileName(String filePath) {
        File file = new File(filePath);
        return file.getName();
    }

    public static String getFilePath(String filePath) {
        File file = new File(filePath);
        return file.getPath();
    }

    public static List<String> getFiles(String folder, String type) {

        File dirFile = new File(folder);

        String[] fileList = dirFile.list();


        List<String> names = new ArrayList<String>();

        for (String str : fileList) {
            String path = dirFile.getAbsolutePath() + MFPath.fseparator + str;

            File file = new File(path);
            if (file.isFile()) {
                if (MFString.isNullOrEmpty(type))
                    names.add(file.getName());
                else if (file.getName().toLowerCase().endsWith(type.toLowerCase())) {
                    names.add(file.getName());
                }

            }
        }
        return names;
    }

}
